package info.xmlprint.ui.navigator;

import info.xmlprint.core.project.PrintProject;
import info.xmlprint.run.RuntimeUtil;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;

/**
 * 
 * 
 *
 */
public class SelectAction extends CommonActionProvider {

	public void fillActionBars(IActionBars actionBars) {
		ISelection selection = getContext().getSelection();
		if (!(selection instanceof IStructuredSelection)) {
			return;
		}

		IStructuredSelection sel = (IStructuredSelection) selection;
		Object obj = sel.getFirstElement();

		if (obj instanceof IResource) {
			IResource res = (IResource) obj;
			IProject project = res.getProject();
			if (project != null) {
				RuntimeUtil rt = RuntimeUtil.getInstance();
				PrintProject pProject = rt.getPrintProject(project);
				if (pProject == null) {
					pProject = new PrintProject(project);
					rt.register(pProject);
				}
				rt.setCurrentPrintProject(pProject);
			}
		}
	}

	@Override
	public void init(ICommonActionExtensionSite anExtensionSite) {
		super.init(anExtensionSite);
	}
}
